<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter="onSubmit()"
    >

      <el-form-item label="用户ID" prop="uid">
        <el-input v-model="dataForm.uid" placeholder="用户ID" />
      </el-form-item>

      <el-form-item label="反馈类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="0"> 投诉 </el-radio>
          <el-radio :label="1"> 建议 </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="反馈内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="反馈内容" type="textarea"  :rows="2"/>
      </el-form-item>

      <el-form-item label="反馈图片" prop="imgs">
        <mul-pic-upload v-model="dataForm.imgs" />
      </el-form-item>


      <el-form-item label="状态" prop="state">
        <el-radio-group v-model="dataForm.state">
          <el-radio :label="0">已提交 </el-radio>
          <el-radio :label="1" disabled> 已回复 </el-radio>
          <el-radio :label="2" disabled> 已评分 </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { Debounce } from "@/utils/debounce";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataForm = ref({
  id: "",
  uid: "",
  type: "",
  content: "",
  imgs: "",
  state:0,
});
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10, // 每页显示多少条
});
const dataRule = {
  imgs: [
    //{ required: true, message: "添加数量不能为空", trigger: "change" },
    //Vue中自定义校验
    {
      validator: (rule, value, callback) => {
        if (dataForm.value.type == 0 &&  (dataForm.value.imgs == null ||dataForm.value.imgs == '' )) {
          callback(new Error("投诉必须上传图片"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};

const dataFormRef = ref(null);
const init = (id) => {
  dataForm.value.id = id || undefined;
  //dataForm.value = { userId: id } || {};
  visible.value = true;
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });
  if (dataForm.value.id) {
    http({
      url: http.adornUrl(`/admin/complaintSuggestions/info/${dataForm.value.id}`),
      method: "get",
      params: http.adornParams(),
    }).then(({ data }) => {
      dataForm.value = data;
    });
  }
};
defineExpose({ init });

/**
 * 表单提交
 */
const onSubmit = Debounce(() => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl("/admin/complaintSuggestions"),
        method: dataForm.value.id ? "put" : "post",
        data: http.adornData({
          ...dataForm.value,
        }),
      }).then(() => {
        ElMessage({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            visible.value = false;
            emit("refreshDataList", page);
          },
        });
      });
    }
  });
});
</script>
